package com.ysxsoft.wdg.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.FragmentAdapter;
import com.ysxsoft.wdg.base.BaseFragment;
import com.ysxsoft.wdg.ui.activity.center_st.StShopDetailActivity;
import com.ysxsoft.wdg.ui.activity.center_zy.ZySearchActivity;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.zxing.ScanActivity;
import com.ysxsoft.wdg.widget.viewpager.NoScrollViewPager;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CenterFragment extends BaseFragment {


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.shiti)
    TextView shiti;
    @BindView(R.id.ziying)
    TextView ziying;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    private int type = 0;//0自营  1实体

    private FragmentAdapter adapter;
    private List<BaseFragment> fragments;

    @Override
    protected int getLayoutId() {
        return R.layout.fm_three;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);

        fragments = new ArrayList<>();
        fragments.add(new ZyFragment());
        fragments.add(new StFragment());

        adapter = new FragmentAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(adapter);

        type = 0;
        ziying.setSelected(true);
        viewPager.setCurrentItem(0);

    }

    @Override
    protected void setListener() {

    }

    @OnClick({R.id.ziying, R.id.shiti, R.id.iv_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ziying:
                if (!view.isSelected()) {
                    type = 0;
                    viewPager.setCurrentItem(0);
                    ziying.setSelected(true);
                    shiti.setSelected(false);
                    ivTitle.setImageResource(R.mipmap.zy_search);
                }
                break;
            case R.id.shiti:
                if (!view.isSelected()) {
                    type = 1;
                    viewPager.setCurrentItem(1);
                    ziying.setSelected(false);
                    shiti.setSelected(true);
                    ivTitle.setImageResource(R.mipmap.home_sao);
                }
                break;
            case R.id.iv_title:
//                ToastUtils.showToast(type == 0 ? "搜索" : "扫码");
                if (type == 0) {
                    startActivity(new Intent(mContext, ZySearchActivity.class));
                } else {
                    Intent intent = new Intent(getActivity(), ScanActivity.class);
                    startActivityForResult(intent, REQUEST_CODE_SCAN);
                }
                break;
        }
    }

    public static final int REQUEST_CODE_SCAN = 0x04;


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_SCAN:
                //扫码结果
                try {
                    if (data != null) {
                        String result = data.getStringExtra("result");
                        if (result != null) {
                            StShopDetailActivity.start(getActivity(),result);
                        }else{
                            ToastUtils.showToast("未扫到任何二维码！");
                        }
                    } else {
                        ToastUtils.showToast("二维码识别失败！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

}
